<template>
	<view class="container">
		<view class="cu-card" style="min-height: 500px;">
			<!-- <view class="logo">
				<image src="../../static/images/app-logo.png"></image>
			</view> -->
			
			<view class="reg_box">
				<view class="reg_box_bg"></view>
				<view class="cu-form-group margin-lr-lg border-b-line padding-0">
					<view class="title">手机号</view>
					<input data-key="phone" placeholder="绑定的手机号码" v-model="phone" disabled="true"/>
				</view>
				<view class="cu-form-group margin-lr-lg border-b-line padding-0">
					<view class="title">验证码</view>
					<input data-key="smscode"  placeholder="请输入验证码" v-model="code" />
					<button class="cu-btn line" style="color:#4d8efd;border:1rpx solid #4d8efd;" @click="getCode">{{message}}</button>
				</view>				
				<view class="cu-form-group margin-lr-lg border-b-line padding-0">
					<view class="title">设置新密码</view>
					<input data-key="password" password  placeholder="请输入新支付密码" v-model="password" :maxlength="type === 'pay' ? 6 : undefined"/>
				</view>
				<view class="cu-form-group margin-lr-lg padding-0">
					<view class="title">重复密码</view>
					<input data-key="repassword" password  placeholder="再次输入支付密码" v-model="repassword" :maxlength="type === 'pay' ? 6 : undefined"/>
				</view>	
			</view>
			<view class="cu-form-group margin-lr-lg padding-0 submit_btn">
				<button class="cu_btn" type="button" @click="doreset">确认修改</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		
		data() {
			return {
				index: 0,
				type:'',
				phone: "",
				code:"",
				oldpassword:"",
				password:"",
				repassword:"",
				message: "获取验证码",
				time: this.helper.time
			};
		},
		onLoad: function (option) {
			this.type = option.type;
			if(this.type == 'login'){
				uni.setNavigationBarTitle({
					title: '修改登录密码'
				});
			}
			if(this.type == 'pay'){
				uni.setNavigationBarTitle({
					title: '修改支付密码'
				});
			}
			this.helper.checkLogin();
			this.getData();
		},
		methods: {
			getData:function(){
				//获取基础参数
				// uni.showLoading({
				// 	title:"数据加载中",
				// 	mask:true
				// })
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['username='+info.username,'token='+info.token,'timestamp='+timestamp]);
				this.helper.req({
					api:"getUserInfo",
					timestamp:timestamp,
					data:{
						username:info.username,
						token:info.token,
					},
					header:{sign:sign},
					method:'GET',
				}).then(res=>{
					this.phone = res.data.data.userinfo.phone;
					setTimeout(() => {
						uni.hideLoading();
					}, 300);
				})
			},
			daojishi() {
				let eventinterval = setInterval(() => {
					this.message = this.time-- + "秒";
					if (this.time <= 0) {
						clearInterval(eventinterval);
						this.message = "获取验证码";
					}
				}, 1000);
			},
			getCode() {
				console.log('sms')
				if(this.phone == ''){
					uni.showToast({
						title:"请输入手机号",
						icon:"none"
					})
					return;
				}
				if (this.message != "获取验证码") {
					return;
				}
				//短信验证码的获取
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(["phone=" + this.phone, "type=repasswd","timestamp=" + timestamp]);
				this.helper.req({
					api:"getcode",
					timestamp:timestamp,
					data:{
						phone: this.phone,
						type:'repasswd'
					},
					header:{
						sign: sign
					}
				}).then(res=>{
					if (this.message == "获取验证码") {
						this.time = this.helper.time;
						this.uniqid= res.data.data.uniqid;
						this.daojishi();
					}
				});
			},
			doreset() {
				// if(this.oldpassword == ''){
				// 	uni.showToast({
				// 		title:"请输入旧密码",
				// 		icon:"none"
				// 	})
				// 	return;
				// }
				if(this.password == ''){
					uni.showToast({
						title:"请输入新密码",
						icon:"none"
					})
					return;
				}
				if(this.repassword == ''){
					uni.showToast({
						title:"请输入确认密码",
						icon:"none"
					})
					return;
				}
				if(this.phone == ''){
					uni.showToast({
						title:"请输入手机号",
						icon:"none"
					})
					return;
				}
				if(this.code == ''){
					uni.showToast({
						title:"请输入验证码",
						icon:"none"
					})
					return;
				}
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['type='+this.type,'uniqid='+this.uniqid,'code='+this.code,'old_password='+this.oldpassword,'username='+this.phone,'password='+this.password,'repassword='+this.repassword,'timestamp='+timestamp,'token='+info.token]);
				this.helper.req({
					api:"repasswd",
					timestamp:timestamp,
					data:{type:this.type,uniqid:this.uniqid,username:this.phone,code:this.code,old_password:this.oldpassword,password:this.password,repassword:this.repassword,token:info.token},
					header:{
						sign: sign
					}
				}).then(res=>{
					//密码重置成功
					uni.showToast({
						title: "密码设置成功",
						icon: "success"
					})
					setTimeout(() => {
						uni.hideLoading();
						uni.redirectTo({
							url: '/pages/user/userinfo',
						});
					}, 300);
				});
			},			
			navToUrl:function(url){
				uni.navigateTo({
					url: url,
				});	
			}
		}
	}
</script>

<style lang="scss">
	page {
		// background-image: url('@/static/images/person/center-bg.png');
		// background-size: 100% 100%;
		// background-repeat: no-repeat;
		// min-height: 200vw;
		color: #56595d;
		background-color: #ddeafd;
		//height:100%;
	}
	.reset_title{
		font-size: 50upx;
		padding: 20upx;
	}
	.cu-form-group{
		background: none;
		height: 60px;
		input{
			color:#353536;
			text-align: right;
		}
	}
	.cu-form-group .title {
		//min-width: 85px;
		color: #353536;
	}
	.logo{
		padding: 80px 0 50px;
		text-align: center;
		image{
			width: 100px;
			height: 100px;
		}
	}
	.lg_img{
		width: 24px;
		height: 24px;
	}
	.reg_box{
		width: 88vw;
		margin: 6vw;
		padding: 8px 0;
		// background: #ffffff;
		border-radius: 6px;
		position: relative;
		
		.reg_box_bg{
			position: absolute;
			width: 100%;
			height: 100%;
			left: 0;
			top: 0;
			background-color: #fff;
			border-radius: 10px;
		}
	}
	.cu_btn{
		background: linear-gradient( 180deg, #9fc2ff 0%, #4f8efc 100%);
		height: 50px;
		width: 70vw;
		color: #fff;
		line-height: 50px;
		font-size: 16px;
		border-radius: 30px;
	}
</style>

